package org.darkforest.common.services.impl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.GoodSearchService;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.model.GoodSearchBean;
import org.darkforest.model.Goods;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.GoodsCommonSearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * good search service implementation
 * Created by Justin on 15/1/31.
 */
@Service
public class GoodSearchServiceImpl implements GoodSearchService {

    @Autowired
    private GoodsMapper mapper;

    @Autowired
    private GoodsCommonMapper goodsCommonMapper;

    @Override
    public GoodSearchBean fuzzySearch(GoodSearchBean searchBean) {
//        if (searchBean == null || StringUtils.isBlank(searchBean.getGoodsName())) {
//            return allGoods(GoodSearchBean.DEFAULT_ALL_GOODS_PARAM);
//        }
        List<Goods> result = mapper.selectByFuzzyName(searchBean);
        searchBean.setResultList(CollectionUtils.isEmpty(result) ? Collections.<Goods>emptyList() : result);
        return searchBean;
    }

    @Override
    public GoodSearchBean allGoods(GoodSearchBean searchBean) {
        List<Goods> result = mapper.queryAllGoods(searchBean);
        searchBean.setResultList(CollectionUtils.isEmpty(result) ? Collections.<Goods>emptyList() : result);
        return searchBean;
    }

    @Override
    public Goods getCartGoodsInfo(Integer goodsId) {
        return mapper.getGoodsById(goodsId);
    }

    @Override
    public List<Goods> selectSellerList(GoodSearchBean searchBean) {
        List<Goods> result = mapper.selectSellerList(searchBean);
        return result;
    }

    /**
     * 根据条件查询GoodsCommon列表
     *
     * @param goodsCommonSearchBean
     * @return
     */
    @Override
    public List<GoodsCommon> listGoodsCommonByCondition(GoodsCommonSearchBean goodsCommonSearchBean) {
        List<GoodsCommon> goodsCommonList = goodsCommonMapper.listGoodsCommonByCondition(goodsCommonSearchBean);
        return goodsCommonList;
    }
}
